package logic

import (
	"context"
	"fmt"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProblemShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProblemShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProblemShowLogic {
	return &ProblemShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProblemShowLogic) ProblemShow(in *zhihurpc.ProblemShowRequest) (*zhihurpc.ProblemShowResponse, error) {
	// todo: add your logic here and delete this line
	var problem mysql.Problem
	mysql.DB.Debug().Where("id = ?", in.ProblemId).Find(&problem)
	problem.Hot = problem.Hot + 1
	err := mysql.DB.Updates(&problem).Error
	if err != nil {
		return nil, fmt.Errorf("热度增加失败")
	}

	return &zhihurpc.ProblemShowResponse{
		ProblemId:    int64(problem.ID),
		ProblemImage: problem.ProblemImage,
		ProblemDesc:  problem.ProblemDesc,
		UserId:       int64(problem.UserId),
		Hot:          int64(problem.Hot),
	}, nil
}
